package utils

import (
	"crypto/sha1"
	"encoding/hex"
	"sort"
)

//CheckSign 检查公众号签名
func CheckSign(signature, timestamp, nonce, token string) bool {
	//获取需要sha1的字符串并放入切片
	arr := []string{timestamp, nonce, token}
	//排序
	sort.Strings(arr)
	//声明一个变量来装合并字符串
	var sha1String string = ""
	for _, v := range arr {
		sha1String += v
	}
	//将字符串sha1并重新赋值给sha1String
	h := sha1.New()
	h.Write([]byte(sha1String))
	sha1String = hex.EncodeToString(h.Sum(nil))
	//最后返回对比的布尔值
	return sha1String == signature
}
